package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleResourceMapper roleResourceMapper;

    /**
     * 角色分页
     *
     * @param roleDto
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageBean<RoleVo> selectPage(RoleDto roleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> page = roleMapper.selectPage(roleDto);
        return PageBean.of(page, RoleVo.class);
    }

    /**
     * 角色添加
     *
     * @param roleDto
     */
    @Override
    public void addRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);
    }

    /**
     * 角色修改
     *
     * @param roleDto
     */
    //但凡涉及到修改操作，都需要加上事务注解
    //但凡涉及到多表的修改操作，都是先删除再添加
    @Override
    @Transactional
    public void updateRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateByPrimaryKeySelective(role);

        //判断是否修改角色对应的资源数据
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())) {
            return;
        }

        //删除原有角色资源中间信息
        roleResourceMapper.deleteByRoleId(role.getId());

        //保存角色资源中间信息
        Arrays.stream(roleDto.getCheckedResourceNos()).forEach(n -> {
            RoleResource resource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(n)
                    .dataState("0") //0启用 1禁用
                    .build();
            roleResourceMapper.insert(resource);
        });
    }

    /**
     * 根据角色查询选中的资源数据
     *
     * @param roleId
     */
    @Override
    public Set<String> findCheckedResources(Long roleId) {
        return roleMapper.selectResourcesByRoleId(roleId);
    }

    /**
     * 删除角色
     *
     * @param roleIds
     */
    @Override
    public void deleteRole(Long[] roleIds) {
        for (Long roleId : roleIds) {
            roleResourceMapper.deleteByRoleId(roleId);
            roleMapper.deleteByPrimaryKey(roleId);
        }
    }

    /**
     * 查询所有
     */
    @Override
    public List<RoleVo> list() {
        List<Role> roles = roleMapper.list();
        return BeanUtil.copyToList(roles, RoleVo.class);
    }

}
